﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UdpPeer.Protocols
{
    /// <summary>
    /// 帮肋类
    /// </summary>
    public class MessagePackegHelper
    {

        private static readonly MessagePackegHelper instance = new MessagePackegHelper();

        /// <summary>
        /// 单例
        /// </summary>
        public static MessagePackegHelper Instance
        {
            get { return MessagePackegHelper.instance; }
        } 


        private MessagePackegHelper()
        {

            //协议集合类型
            var procols = GetType().Assembly.GetTypes().Where(p => p.GetInterface(typeof(IProtocol).FullName) != null);

            foreach (Type item in procols)
            {
                IProtocol p = Activator.CreateInstance(item) as IProtocol;
                _MessageDickeg.TryAdd((int)p.消息类型, p);
            }
        }

        /// <summary>
        /// 所有实现的协议
        /// </summary>
        private System.Collections.Concurrent.ConcurrentDictionary<int, IProtocol> _MessageDickeg = new System.Collections.Concurrent.ConcurrentDictionary<int, IProtocol>();
        /// <summary>
        /// 所有实现的协议
        /// </summary>
        public System.Collections.Concurrent.ConcurrentDictionary<int, IProtocol> MessageDickeg
        {
            get { return _MessageDickeg; }
        }

        /// <summary>
        /// 返回序反序列化数据
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="json">数据</param>
        /// <returns></returns>
        public T GetEntity<T>(string json)
        {

            MessagePack basePackeg = Newtonsoft.Json.JsonConvert.DeserializeObject<MessagePack>(json);
            //返回 数据
            if (!string.IsNullOrEmpty(basePackeg.MessageContent))
            {
                if (MessageDickeg.ContainsKey(basePackeg.MessageType))
                {
                    return (T)Newtonsoft.Json.JsonConvert.DeserializeObject(basePackeg.MessageContent, MessageDickeg[basePackeg.MessageType].GetType());
                }
                else
                {
                    return default(T);
                }

            }
            else
            {
                return default(T);
            }

        }

        /// <summary>
        /// 打包一个协议类
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public MessagePack Pakceg(IProtocol ent)
        {
            return new MessagePack()
            {
                MessageType = (int)ent.消息类型,
                MessageContent = Newtonsoft.Json.JsonConvert.SerializeObject(ent)
            };
        }
        /// <summary>
        /// 返回用于发送的JSON字符串
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public string GetJsonString(IProtocol ent)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(Pakceg(ent));
        }
        /// <summary>
        /// 获取发送协议数据
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public string GetSendProtocolString(IProtocol ent)
        {
            return GetJsonString(ent) + (char)1;
        }
        /// <summary>
        /// 返回接收的数据信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string[] GetReceiveProtocolString(string data)
        {

            return data.Split(new char[] { (char)1 }, StringSplitOptions.RemoveEmptyEntries);
        }

    }
}
